package com.appspot.checkflames.service;

import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.appspot.checkflames.bean.AuthToken;

public class OAuthTokenServiceImpl {
	
	private PersistenceManager mPersistenceManager;
	
	public AuthToken fetchAccessToken(String uid) {
		AuthToken token = null;
		Date date = new Date();
		mPersistenceManager = SingletonPersistenceManagerFactory.get().getPersistenceManager();
		
		Query query = mPersistenceManager.newQuery(AuthToken.class,"token.uid == '" + uid +"' && token.expireDate <= " + date);
		token = (AuthToken) query.execute();
		mPersistenceManager.close();
		return token;
	}
	
	public void persistAccessToken(AuthToken persistToken) {
		mPersistenceManager = SingletonPersistenceManagerFactory.get().getPersistenceManager();
		mPersistenceManager.makePersistent(persistToken);
		mPersistenceManager.close();
	}
}
